package com.aspirecn.core.datacache.useragent;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

import com.aspire.core.match.ua.object.DeviceGWUA;
import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.datacache.BaseLocalCache;
import com.csvreader.CsvReader;

/**
 * devicegwua数据缓存
 * 
 * @author wangxinning
 * 
 */
public class DeviceGWUACache extends BaseLocalCache {

	public final static Logger logger = Logger.getLogger(DeviceGWUACache.class);

	// private Map<Integer, DeviceUA> deviceUAMap = new HashMap<Integer,
	// DeviceUA>();
	private List<DeviceGWUA> deviceGWUAList = new ArrayList<DeviceGWUA>();

	private static DeviceGWUACache instance = new DeviceGWUACache();

	private DeviceGWUACache() {

		this.dataFile = new File(Configuration.getInstance()
				.getDownloadFilePath()
				+ File.separator
				+ "uaV3"
				+ File.separator + "devicegwua.csv");
		load();
	}

	public static DeviceGWUACache getInstance() {
		return instance;
	}

	public void load() {

		if (!loading.getAndSet(true)
				&& Configuration.getInstance().switchUACache()
				&& this.dataFile.exists()) {

			this.clear();

			long start = System.currentTimeMillis();

			try {

				CsvReader reader = new CsvReader(this.dataFile.toString(), ',',
						Charset.forName("gbk"));

				while (reader.readRecord()) {

					if (reader.getCurrentRecord() == 0)// 设置列名
					{
						reader.setHeaders(reader.getValues());
					} else {
						DeviceGWUA gwua = new DeviceGWUA();
						gwua.setDevice_id(NumberUtils.toInt(reader
								.get("DEVICE_ID")));
						gwua.setDevice_gw_ua(StringUtils.remove(StringUtils
								.remove(reader.get("DEVICE_GW_UA"), "(null)"),
								"\n"));

						deviceGWUAList.add(gwua);
					}
				}

				reader.close();

			} catch (Exception e) {
				logger.error("DeviceGWUA信息加载异常", e);
			}

			loading.set(false);
			long end = System.currentTimeMillis();
			updateFileDownloadTime();
			logger.info("DeviceGWUA信息加载完成，time=" + (end - start) + "ms,size="
					+ deviceGWUAList.size());

		} else {
			loading.set(false);
			logger.info("配置关闭，DeviceGWUA信息未加载");
		}

		loading.set(false);
	}

	@Override
	public void clear() {

		deviceGWUAList.clear();
	}

	@Override
	public void reload() {

		if (this.fileIsChange()) {
			this.load();
		} else {
			logger.info("没变化，DeviceGWUA信息未加载");
		}
	}

	public List<DeviceGWUA> getDeviceUA(int deviceid) {
		List<DeviceGWUA> retList = new ArrayList<DeviceGWUA>();
		for (DeviceGWUA gwua : deviceGWUAList) {
			if (gwua.getDevice_id() == deviceid) {
				retList.add(gwua);
			}
		}
		return retList;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		DeviceGWUACache geoCache = DeviceGWUACache.getInstance();
		System.out.println(geoCache.getDeviceUA(7356));
		long end = System.currentTimeMillis();

		System.out.println(end - start);

	}
}